﻿using System;
using System.Web.UI;
using Kiiro.Common.Utility;
using Kiiro.SiteDefinition.ControlTemplates;
using Kiiro.SiteDefinition.Core;
using Kiiro.Model;
using Microsoft.SharePoint;
using System.Web.UI.HtmlControls;


namespace Kiiro.SiteDefinition.MasterPage
{
    public partial class KiiroMaster : System.Web.UI.MasterPage
    {
        public Header V2Header
        {
            get { return ( Header ) header; }
        }

        public NavigationMain Footer
        {
            get { return ( NavigationMain ) navigationMain; }
        }

        public NavigationSub NavigationSub
        {
            get { return ( NavigationSub ) navigationSub; }
        }

        public ErrorsList V2ErrorsList
        {
            get { return ( ErrorsList ) errorsList; }
        }

        private BasePage basePage;
        public BasePage Base
        {
            get { return basePage; }
        }

        protected override void OnInit(EventArgs e)
        {
            basePage = Page is BasePage ? (BasePage)Page : new BasePage().DefaultBasePage;
            customCssLink.Href = SPHelpers.GetPortalServerRelativeUrl(Base.SiteCollectionId, Base.ServerRelativeUrl).TrimEnd('/') + "/kiiro-custom.css";
            base.OnInit(e);
        }

        protected void Page_Load ( object sender, EventArgs e )
        {
            ApplyTheme ( );
            litPageType.Text = SPHelpers.IsPortal ( Base.SiteCollectionId, Base.ServerRelativeUrl ) ? "main" : "project";

            using ( SPWeb web = SPHelpers.GetWeb ( Base.SiteCollectionId, Base.ServerRelativeUrl ) )
            {
                SPList list = web.GetList ( Request.Url.ToString ( ) );
                if ( list != null && !Base.CurrentUser.IsAdministrator )
                {
                    foreach ( SPView view in list.Views )
                    {
                        if ( Request.Url.ToString ( ).Contains ( view.Url ) &&
                            ( list.BaseTemplate.ToString ( ) == "50000" ||
                            list.BaseTemplate.ToString ( ) == "50001" ||
                            list.BaseTemplate.ToString ( ) == "50002" ||
                            list.BaseTemplate.ToString ( ) == "50003" ||
                            list.BaseTemplate.ToString ( ) == "50004" ||
                            list.BaseTemplate.ToString ( ) == "50005" ||
                            list.BaseTemplate.ToString ( ) == "50006" ||
                            list.BaseTemplate.ToString ( ) == "50007" ) )
                        {
                            Response.Redirect ( Base.ServerRelativeUrl + "/_layouts/accessdenied.aspx?Source=" + Base.ServerRelativeUrl );
                        }
                    }
                }
            }

            lblYear.Text = DateTime.Today.Year.ToString ( );
            lblVersion.Text = "v. " + Constants.AssemblyInfo.Version;
        }

        private void ApplyTheme()
        {            
            string themeCssUrl = null;
            using (SPWeb web = SPHelpers.GetWeb(Base.SiteCollectionId, Base.ServerRelativeUrl))
            {
                themeCssUrl = web.ThemeCssUrl;
            }
            if (!string.IsNullOrEmpty(themeCssUrl))
            {
                HtmlHead head = (HtmlHead)Page.Header;
                HtmlLink link = new HtmlLink();
                link.Attributes.Add("href", Page.ResolveClientUrl(themeCssUrl));
                link.Attributes.Add("type", "text/css");
                link.Attributes.Add("rel", "stylesheet");
                head.Controls.Add(link);
            }           
        }

    }
}

/*************************************************************************
 * 
 * $Log: /Internal Projects/Kiiro/Kiiro.SiteDefinition/12/TEMPLATE/FEATURES/Kiiro.MasterPage/MasterPage/Kiiro.Master.cs $
 * 
 * 28    2/08/10 5:04p Camerons
 * 
 * 27    2/08/10 2:28p Camerons
 * 
 * 26    1/25/10 1:17p Camerons
 * 
 * 25    1/12/10 3:19p Camerons
 * 
 * 24    1/12/10 12:03p Camerons
 * 
 * 23    1/08/10 11:30a Camerons
 * 
 * 22    1/07/10 11:20a Camerons
 * 
 * 21    1/06/10 12:39p Camerons
 * 
 * 20    4/01/10 12:03p Adamw
 * 
 * 19    12/21/09 5:06p Camerons
 * 
 * 18    12/18/09 11:34a Camerons
 * 
 * 17    12/17/09 3:45p Stefanf
 * 
 * 16    12/16/09 4:05p Stefanf
 * 
 * 15    26/11/09 10:35a Camerons
 * 
 * 14    17/11/09 6:46p Camerons
 * 
 * 13    3/11/09 12:50p Camerons
 * 
 * 12    31/10/09 1:05p Camerons
 * 
 * 11    28/10/09 10:32p Camerons
 * 
 * 10    23/09/09 4:46p Camerons
 * Fixed some old code dealing with User.ManageProjects
 * 
 * 9     22/09/09 6:03p Camerons
 * Code changes to support creating user on demand - version 1.1.x.x
 * 
 * 8     21/09/09 3:30p Camerons
 * First batch of changes for 1.1.x.x - Security groups update
 * 
 * 7     8/31/09 3:36p Stefanf
 * 
 * 6     17/08/09 11:07a Camerons
 * Added check-in comments section
 * 
 ************************************************************************/